package za.um.td.state.client;

import java.util.logging.Logger;

import za.um.td.entity.level.Terrain;
import za.um.td.entity.level.gui.LevelGui;
import za.um.td.entity.manager.LevelEntityManager;
import za.um.td.entity.mouse.MouseEntity;
import za.um.td.state.BasicGameStateSingleton;

import com.jme.scene.Spatial;
import com.jme.scene.state.CullState;
import com.jme.system.DisplaySystem;
import com.jmex.game.state.BasicGameStateNode;
import com.jmex.game.state.GameState;

public class LevelRenderStateNode extends BasicGameStateNode<GameState> {
	private static final Logger LOG = Logger
			.getLogger(LevelRenderStateNode.class.getName());

	private static final String NAME = "LevelRenderStateNode";
	private static LevelRenderStateNode instance = null;

	private DisplaySystem display = null;

	private MouseEntity mouse;

	private LevelRenderStateNode() {
		super(NAME);

		
		
		// Set display
		display = BasicGameStateSingleton.game.getDisplay();

		// Set culling
		rootNode.setCullHint(Spatial.CullHint.Never);
		CullState cs = display.getRenderer().createCullState();
		cs.setCullFace(CullState.Face.Back);
		rootNode.setRenderState(cs);
		

		// Set entities
		rootNode.attachChild(LevelEntityManager.getInstance().getEntityManagerNode());
		// XXX: The below shouldn't need to be added. Find a way to remove it
		// and put it in Terrain class.
		LevelEntityManager.getInstance().getTerrain().getSpatial()
				.updateRenderState();

		// Set the mouse
		mouse = ClientStateNode.getInstance().getMouse();
		rootNode.attachChild(mouse.getSpatial());
		
		
	}

	public static LevelRenderStateNode getInstance() {
		if (instance == null) {
			instance = new LevelRenderStateNode();
		}
		return instance;
	}

	public void render(float tpf) {
		super.render(tpf);
		DisplaySystem.getDisplaySystem().getRenderer().draw(rootNode);

		// This HAS to be called, otherwise the mouse breaks
		mouse.getStrategicHandler().useCursor("default");
	}

}
